package org.millioncall.yueyoga.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Role;
import org.millioncall.yueyoga.admin.service.AuthorityService;
import org.millioncall.yueyoga.admin.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

//--created by lisj on 2014.03.06
@Controller
public class RoleAction extends BaseAction {

	private RoleService roleService;
	private AuthorityService authorityService;
	/** 查询角色 **/
	@RequestMapping(value = "/findRoleUsers.jhtml")
	public String findRoleUsers(String userName, String inherit,
			String description, Integer enabled, String pageNo,
			String pageSize, ModelMap map, HttpServletRequest request) {
		super.initFrontHeader(request, map);

		int start = 0;
		int limit = 10;

		if (StringUtils.isNotBlank(pageSize)) {
			limit = Integer.parseInt(pageSize);
		}

		if (StringUtils.isNotBlank(pageNo)) {
			start = (Integer.parseInt(pageNo) - 1) * limit;
		}
		
		if (StringUtils.isNotBlank(userName)) {
			map.put("userName", userName);
		}
		if (StringUtils.isNotBlank(inherit)) {
			map.put("inherit", inherit);
		}
		if (StringUtils.isNotBlank(description)) {
			map.put("description", description);
		}
		if (enabled != null) {
			map.put("enabled", enabled);
		}
		PaginatedList<Role> roles = roleService.getRolesInPage(start, limit,
				map);
		map.put("roles", roles);
		//--获取已经启用的权限
		map.put("authorities", authorityService.findByEnabled(true));
		return "user_role_search";
	}

	/** 删除角色 **/
	@RequestMapping(value = "/deleteRole.jhtml")
	public void deleteRoles(HttpServletRequest request,
			HttpServletResponse response) {
		String msg = null;
		String ids = request.getParameter("ids");
		if (StringUtils.isBlank(ids)) {
			msg = "{'success':false,'msg':'未提交删除角色'}";
		} else {
			String[] arrids = ids.split(",");
			int[] id = new int[arrids.length];
			for (int i = 0; i < arrids.length; i++) {
				if (arrids[i] != null && !arrids[i].equals("")) {
					id[i] = Integer.parseInt(arrids[i]);
				}
			}
			try {
				roleService.deleteRoleByIds(id);// 删除角色
				roleService.deleteRoleGroupById(id);// 删除角色跟组的关联
				msg = "{'success':true,'msg':'角色删除成功!'}";
			} catch (RuntimeException re) {
				msg = "{'success':false,'msg':'角色删除失败!'}";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);

	}

	/** 进入添加角色 **/

	@RequestMapping(value = "/toAddRoleUser.jhtml")
	public String toAddRolrUser(ModelMap map, HttpServletRequest request) {
		super.initFrontHeader(request, map);
		return "user_role_add";
	}

	/** 添加角色 **/
	@RequestMapping(value = "/addRoleUser.jhtml")
	public void addRoleUser(String userName, String userInherit,
			String description, Integer enabled, ModelMap map,
			HttpServletRequest request, HttpServletResponse response) {
		super.initFrontHeader(request, map);
		String msg = "";
		Role role = roleService.findRoleByName(userName);
		if(role!=null){
			msg = "{'cunzai':true, 'msg':'该角色已经存在!!'}";
			WebAppUtils.ajaxResponse(response, msg);
			return ;
		}
		if (StringUtils.isNotBlank(userName)) {
			map.put("userName", userName);
		}
		if (StringUtils.isNotBlank(userInherit)) {
			map.put("userInherit", userInherit);
		}
		if (StringUtils.isNotBlank(description)) {
			map.put("description", description);
		}
		if (enabled != null) {
			map.put("enabled", enabled);
		}
		if (roleService.saveRole(map)) {
			msg = "{'success':true, 'msg':'角色创建成功!'}";
		} else {
			msg = "{'success':false, 'msg':'角色创建失败!'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	/** 进入修改角色 **/
	@RequestMapping(value = "/toUpdateRoleUser.jhtml")
	public String toUpdateRoleUser(Integer roleId, ModelMap map,
			HttpServletRequest request) {
		super.initFrontHeader(request, map);
		if (roleId != null) {
			Role role = roleService.findRoleById(roleId);
			map.put("role", role);
		}
		return "user_role_edit";
	}

	/** 修改角色 **/
	@RequestMapping(value = "/updateRoleUser.jhtml")
	public void updateRoleUser(String userName, String inherit, String roleId,
			String description, String enabled, ModelMap map,
			HttpServletRequest request, HttpServletResponse response) {
		super.initFrontHeader(request, map);

		String msg = "";
		if (StringUtils.isNoneBlank(roleId)) {
			map.put("roleId", roleId);
		}
		if (StringUtils.isNotBlank(userName)) {
			map.put("userName", userName);
		}
		if (StringUtils.isNotBlank(inherit)) {
			map.put("inherit", inherit);
		}
		if (StringUtils.isNotBlank(description)) {
			map.put("description", description);
		}
		if (StringUtils.isNotBlank(enabled)) {
			map.put("enabled", enabled);
		}
		if (roleService.updateRole(map)) {
			msg = "{'success':true, 'msg':'角色修改成功!'}";
		} else {
			msg = "{'success':false, 'msg':'角色修改失败!'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	/** 指定权限 **/
	@RequestMapping(value = "/grantRoleAuthorities.jhtml")
	public void grantRoleAuthorities(String roleId, String auIds,
			ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, model);
		String msg = "";
		if (roleService.grantRoleAuthorities(roleId,auIds)) {
			msg = "{'success':true, 'msg':'角色权限指定成功!'}";
		} else {
			msg = "{'success':false, 'msg':'角色权限指定失败!'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public AuthorityService getAuthorityService() {
		return authorityService;
	}

	public void setAuthorityService(AuthorityService authorityService) {
		this.authorityService = authorityService;
	}
}
